import Vue from "vue";
import Router from "vue-router";
import Login from "../components/login";
import Register from "../components/register";
import Main from "../components/main";
import Upload from "../components/upload";
import Month from "../components/month";
import Sum from "../components/sum";
import InfoTable from "@/components/infoTable";
import Reset from "@/components/reset";
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      name: "Login",
      component: Login
    },
    {
      path: "/register",
      name: "Register",
      component: Register
    },
    {
      path: "/main",
      name: "Main",
      redirect: "/infoTable",
      component: Main,
      children: [
        {
          path: "/infoTable",
          name: "InfoTable",
          component: InfoTable
        },
        {
          path: "/month",
          name: "Month",
          component: Month
        },
        {
          path: "/sum",
          name: "Sum",
          component: Sum
        },
        {
          path: "/upload",
          name: "Upload",
          component: Upload
        }
      ]
    },
    {
      path: "/reset",
      name: "Reset",
      component: Reset
    }
  ]
});
import axios from "axios";
const routerDictionary = ["Login", "Logout", "Register", "Reset"];
router.beforeEach((to, from, next) => {
  if (routerDictionary.includes(to.name)) {
    sessionStorage.clear();
    next();
  } else {
    const token = sessionStorage.getItem("token");
    if (token) {
      next();
    } else {
      next({ name: "Login", replcae: true });
    }
  }
});
export default router;
